/**
 * Created by 又、又 on 2017/8/3.
 */

$(function () {

    //点击勾选事件
    $(document).on("click",".serial",function () {
        var that=this;
        $(that).toggleClass("colour");
        if ($(that).find("input").is(':checked')) {
            $(that).find("input").prop("checked",false);
        }else{
            $(that).find("input").prop("checked",true);
        }
    });
    // 加载列表
    add_list();
    //上一页
    $(".prePage").click(function () {
        var prePage_val=$(this).attr("data-prePage");
        var total_page=$('.totalPage').text();
        if(prePage_val<total_page){
            $('.right_box .arrow_fl .fa').removeClass('right_box_act')
        }
        var page_size=$('#select').val();
        add_list('','','','','','','','','',prePage_val,page_size,'','')
    });
    // 下一页
    $(".nextPage").click(function () {
        var nextPage_val=$(this).attr("data-nextpage");
        var total_page=$('.totalPage').text();
        if(nextPage_val==total_page){
            $('.right_box .arrow_fl .fa').addClass('right_box_act')
        }
        var page_size=$('#select').val();
        add_list('','','','','','','','','',nextPage_val,page_size,'','')
    });
    //第一页
    $(".hasPrePage").click(function () {
        add_list('','','','','','','','','',1,'','','')
    });
    // 最后一页
    $(".last_page").click(function () {
        var last_page_val=$('.totalPage').text();
        if(last_page_val==last_page_val){
            $('.right_box .arrow_fl .fa').addClass('right_box_act')
        }
        var page_size=$('#select').val();
        add_list('','','','','','','','','',last_page_val,page_size,'','')
    });
    //页面搜索
    $(".currentPage").blur(function () {
        var currentPage=parseInt($(this).val());
        var totalPage=parseInt($(".totalPage").text());
        if(currentPage>totalPage){
            alert("不能大于总页数")
        }else {
            add_list('','','','','','','','','',currentPage,'','','')
        }
    });
    //序列号的选择
    $("#select").on('change',function () {
        var currentPage=$('.currentPage').val();
        if($(this).val()==='10'){
            var variable=parseInt(10*currentPage/10);
            if(variable==0){
                variable=1;
            }
            add_list('','','','','','','','','',variable,10,'','');
            $('.digit_num').text("1");
            $('.digit_ber').text("10");

        }else if($(this).val()==='20'){
            var variable=parseInt(10*currentPage/20);
            if(variable==0){
                variable=1;
            }
            add_list('','','','','','','','','',variable,20,'','');
            $('.digit_num').text("11");
            $('.digit_ber').text("20");
        }else if($(this).val()==='30'){
            var variable=parseInt(10*currentPage/30);
            if(variable==0){
                variable=1;
            }
            add_list('','','','','','','','','',variable,30,'','');
            $('.digit_num').text("21");
            $('.digit_ber').text("30");
        }

    });

    //所属区域显示隐藏图标
    $('.send_region').on('click',function () {
        $(this).addClass('box_con').siblings().removeClass('box_con');
        $('.box_con .fa').toggleClass("ashy");
        var sort='cityCode';
        var pageNo=$('.currentPage').val();
        var pageSize=$('#select').val();
        if(!$(this).find(".fa-sort-asc").hasClass("ashy")){
            var order='ASC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
        if(!$(this).find(".fa-sort-desc").hasClass("ashy")){
            var order='DESC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }

    });
    //公司名称显示隐藏图标
    $('.send_company').on('click',function () {
        $(this).addClass('box_con').siblings().removeClass('box_con');
        $('.box_con .fa').toggleClass("ashy");
        var sort='cityCode';
        var pageNo=$('.currentPage').val();
        var pageSize=$('#select').val();
        if(!$(this).find(".fa-sort-asc").hasClass("ashy")){
            var order='ASC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
        if(!$(this).find(".fa-sort-desc").hasClass("ashy")){
            var order='DESC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
    });
    //小区名称显示隐藏图标
    $('.send_village').on('click',function () {
        $(this).addClass('box_con').siblings().removeClass('box_con');
        $('.box_con .fa').toggleClass("ashy");
        var sort='cityCode';
        var pageNo=$('.currentPage').val();
        var pageSize=$('#select').val();
        if(!$(this).find(".fa-sort-asc").hasClass("ashy")){
            var order='ASC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
        if(!$(this).find(".fa-sort-desc").hasClass("ashy")){
            var order='DESC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
    });
    //门禁名称显示隐藏图标
    $('.send_entrance').on('click',function () {
        $(this).addClass('box_con').siblings().removeClass('box_con');
        $('.box_con .fa').toggleClass("ashy");
        var sort='cityCode';
        var pageNo=$('.currentPage').val();
        var pageSize=$('#select').val();
        if(!$(this).find(".fa-sort-asc").hasClass("ashy")){
            var order='ASC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
        if(!$(this).find(".fa-sort-desc").hasClass("ashy")){
            var order='DESC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
    });
    //安装人显示隐藏图标
    $('.send_Installer').on('click',function () {
        $(this).addClass('box_con').siblings().removeClass('box_con');
        $('.box_con .fa').toggleClass("ashy");
        var sort='cityCode';
        var pageNo=$('.currentPage').val();
        var pageSize=$('#select').val();
        if(!$(this).find(".fa-sort-asc").hasClass("ashy")){
            var order='ASC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
        if(!$(this).find(".fa-sort-desc").hasClass("ashy")){
            var order='DESC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
    });
    //联系电话显示隐藏图标
    $('.send_phone').on('click',function () {
        $(this).addClass('box_con').siblings().removeClass('box_con');
        $('.box_con .fa').toggleClass("ashy");
        var sort='cityCode';
        var pageNo=$('.currentPage').val();
        var pageSize=$('#select').val();
        if(!$(this).find(".fa-sort-asc").hasClass("ashy")){
            var order='ASC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
        if(!$(this).find(".fa-sort-desc").hasClass("ashy")){
            var order='DESC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
    });
    //安装时间显示隐藏图标
    $('.send_time').on('click',function () {
        $(this).addClass('box_con').siblings().removeClass('box_con');
        $('.box_con .fa').toggleClass("ashy");
        var sort='cityCode';
        var pageNo=$('.currentPage').val();
        var pageSize=$('#select').val();
        if(!$(this).find(".fa-sort-asc").hasClass("ashy")){
            var order='ASC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
        if(!$(this).find(".fa-sort-desc").hasClass("ashy")){
            var order='DESC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
    });
    //最近动态显示隐藏图标
    $('.send_trends').on('click',function () {
        $(this).addClass('box_con').siblings().removeClass('box_con');
        $('.box_con .fa').toggleClass("ashy");
        var sort='cityCode';
        var pageNo=$('.currentPage').val();
        var pageSize=$('#select').val();
        if(!$(this).find(".fa-sort-asc").hasClass("ashy")){
            var order='ASC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
        if(!$(this).find(".fa-sort-desc").hasClass("ashy")){
            var order='DESC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
    });
    //状态显示隐藏图标
    $('.send_state').on('click',function () {
        $(this).addClass('box_con').siblings().removeClass('box_con');
        $('.box_con .fa').toggleClass("ashy");
        var sort='cityCode';
        var pageNo=$('.currentPage').val();
        var pageSize=$('#select').val();
        if(!$(this).find(".fa-sort-asc").hasClass("ashy")){
            var order='ASC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
        if(!$(this).find(".fa-sort-desc").hasClass("ashy")){
            var order='DESC';
            add_list('','','','','','','','','',pageNo,pageSize,sort,order)
        }
    });
    //请选开门方式
    $('.search_main').on('click',function () {
        $(this).siblings('.search_main_box_list').toggle();

    });
    //点击赋值给.con_empty_input
    $('.search_main_box_list li').on('click',function () {
        var htm=$(this).html();
        $(this).parent('.search_main_box_list').siblings('.search_main').find('.search_box_content').html(htm);
        //$('.con_empty_input').removeClass("empty");
        $('.search_main_box_list').hide()
    });

    //查询
    $(".query ").click(function () {

        var agtCode=$('.company-head').attr('data-sid');
        var communityId=$('.community-head').attr('data-sid');
        var areaCode=$('.city_val').attr('data-code');
        var ptimeStart1=$('#control_date').val();
        var ptimeEnd1=$('#control_date_a').val();
        var userName=$('.text_input').val();
        var openType=$('.opening').attr('data-openType');
        var cbName=$('.list_input').val();
        var tel=$('.daily_input').val();
        add_list(agtCode,communityId,areaCode,ptimeStart1,ptimeEnd1,userName,openType,cbName,tel,'','','','');
    });
    /*清空*/
    $('.clear_empty').on('click',function () {
        var company_txt='请选择公司';
        var community_txt='请选择社区';
        var area_txt='请选择地区';
        var content_txt='请选择开门方式';

        $('#control_date').val('');
        $('#control_date_a').val('');
        $('.search_box_content').val('');
        $('.list_input').val('');
        $('.daily_input').val('');
        /*清空后选项框恢复原状态*/

        $('.company-head').html(company_txt);
        $('.community-head').html(community_txt);
        $('.area-head').html(area_txt);
        $('.search_box_content').html(content_txt);

        add_list();

    });

    /*开门方式下拉列表*/
    $('.search_main_box_list li').on('click',function () {
        var openType =$(this).attr('data-openType');
        $(".opening").attr('data-openType',openType);

    });
});

function add_list(agtCode,communityId,areaCode,ptimeStart1,ptimeEnd1,userName,openType,cbName,tel,pageNo,pageSize,sort,order) {
    $.ajax({
        url:global_url+'admin/openlog/page.json',
        dataType:'json',
        xhrFields:{withCredentials:true,useDefaultXhrHerder:false},
        type:'GET',
        async:false,
        data:{agtCode:agtCode,communityId:communityId,areaCode:areaCode,ptimeStart1:ptimeStart1,ptimeEnd1:ptimeEnd1,userName:userName,openType:openType,cbName:cbName,tel:tel,pageNo:pageNo,pageSize:pageSize,sort:sort,order:order},
        success:function (data) {
            var status=data.code;
            if(status==1) {
                var message_len = data.data.length;
                if (message_len == 0) {
                    var _html = '<img src="image/no-data.png">';
                    $(".delivery_data").addClass('img_state');
                    $(".delivery_data").html(_html)
                    $('.flip_right').hide();
                } else {
                    $('.flip_right').show();
                    var _html = '';
                    for (var x = 0; x < message_len; x++) {
                        var cityCode = data.data[x].cityCode;
                        var agtName = data.data[x].agtName;
                        var communityName = data.data[x].communityName;
                        var installName = data.data[x].installName;
                        var aUname = data.data[x].aUname;
                        var tel = data.data[x].tel;
                        var status_rev = data.data[x].status;
                        var openType = data.data[x].openType;
                        var oTime = data.data[x].oTime / 1000;

                        if (cityCode == null) {cityCode = '';}
                        if (agtName == null) {agtName = '';}
                        if (communityName == null) {communityName = '';}

                        if (installName == null) {installName = '';}
                        if (status_rev == null) {status_rev = '';}
                        else if (status_rev == 0) {status_rev = '未安装';}
                        else if (status_rev == 1) {status_rev = '已安装';}
                        else if (status_rev == -1) {status_rev = '未发货';}
                        else if (status_rev == 2) {status_rev = '锁厂测试安装';}

                        if (openType == null) {openType = '';}
                        else if (openType == 0) {openType = '访客微信开门记录';}
                        else if (openType == 1) {openType = '认证用户微信开门记录';}
                        else if (openType == 2) {openType = 'app开门记录';}
                        else if (openType == 3) {openType = '指纹开门';}
                        else if (openType == 4) {openType = '密码开门';}
                        else if (openType == 5) {openType = '门禁卡';}
                        else if (openType == 6) {openType = '机械钥匙';}

                        _html +=
                            '<div class="right_box_cen serial journal">' +
                            '<span class="con-1">' + (x + 1) + '</span>' +
                            '<span class="con-1"><input type="checkbox" name="stuCheckBox"></span>' +
                            '<span class="con-7 oper" title=' + cityCode + '>' + cityCode + '</span>' +
                            '<span class="con-7 oper" title=' + agtName + '>' + agtName + '</span>' +
                            '<span class="con-7 oper" title=' + communityName + '>' + communityName + '</span>' +
                            '<span class="con-7 oper" title=' + installName + '>' + installName + '</span>' +
                            '<span class="con-7 oper" title=' + aUname + '>' + aUname + '</span>' +
                            '<span class="con-7 oper" title=' + tel + '>' + tel + '</span>' +
                            '<span class="con-7 oper" title=' + status_rev + '>' + status_rev + '</span>' +
                            '<span class="con-7 oper" title=' + openType + '>' + openType + '</span>' +
                            '<span class="con-7 oper" title=' + getLocalTime(oTime) + '>' + getLocalTime(oTime) + '</span>' +
                            '</div>';
                    }
                    $(".delivery_data").removeClass('img_state');
                    $(".delivery_data").html(_html);
                    // 翻页部分
                    var totalPage = data.page.totalPage;
                    var currentPage = data.page.currentPage;
                    var total = data.total;
                    var nextPage = data.page.nextPage;
                    var prePage = data.page.prePage;

                    var firstPage = data.page.firstPage;
                    var hasNextPage = data.page.hasNextPage;
                    var hasPrePage = data.page.hasPrePage;
                    var lastPage = data.page.lastPage;
                    if (firstPage == true) {
                        $('.prePage ').addClass('right_box_act');
                    } else {
                        $('.prePage ').removeClass('right_box_act');
                    }
                    if (hasPrePage == false) {
                        $('.hasPrePage  ').addClass('right_box_act');
                    } else {
                        $('.hasPrePage  ').removeClass('right_box_act');
                    }
                    if (prePage == 0) {
                        $('.right_box .arrow .fa').addClass('right_box_act');
                        $('.right_box .arrow_fl .fa').removeClass('right_box_act');
                    }

                    $(".totalPage").text(totalPage);
                    $(".currentPage").val(currentPage);
                    $(".total").text(total);
                    $(".nextPage").attr('data-nextpage', nextPage);
                    $(".prePage").attr('data-prePage', prePage);
                }

            }else {
                layer.msg('数据加载失败', {icon: 5});
            }
        },
        error:function(data){
            alert(JSON.stringify(data));
        }
    });
}


$('.company-head-box').on('click',function () {
    filtrate_list();
});
function filtrate_list(agtName) {
    $.ajax({
        url:global_url+'admin/tagent/selectList.json',
        dataType:'json',
        xhrFields:{withCredentials:true,useDefaultXhrHerder:false},
        type:'GET',
        async:false,
        data:{agtName:agtName},
        success:function (data) {
            var status=data.code;
            if(status==1){
                var leng=data.data.length;
                var htm_='';
                for(var i=0;i<leng;i++){
                    var uName =data.data[i].agtName;
                    var sId =data.data[i].sId;
                    htm_+='<li data-sId='+sId+' data-uname='+uName+'>'+uName+'</li>';
                }
                $('.select_company').append(htm_);
                $('.company_data').append(htm_);

            }else {
                layer.msg('数据加载失败', {icon: 5});
            }
        }

    });
}
$(document).on('click','.select_company li',function () {
    var uName =$(this).attr('data-uname');
    var sId =$(this).attr('data-sId');
    $(".company-head").attr('data-comapy',uName);
    $(".company-head").attr('data-sId',sId);

});
/*小区名称下拉列表*/
$('.community-head-box').on('click',function () {
    plot_list();
});
function plot_list(agtCode) {
    $.ajax({
        url:global_url+'admin/tcommunity/selectList.json',
        dataType:'json',
        xhrFields:{withCredentials:true,useDefaultXhrHerder:false},
        type:'GET',
        async:false,
        data:{agtCode:agtCode},
        success:function (data) {
            var status=data.code;
            if(status==1){
                var leng=data.data.length;
                var htm_='';
                for(var i=0;i<leng;i++){
                    var communityName =data.data[i].communityName;
                    var sId =data.data[i].sId;
                    htm_+='<li data-sId='+sId+' data-communityName='+communityName+'>'+communityName+'</li>';
                }
                $('.select_plot').append(htm_);

            }else {
                layer.msg('数据加载失败', {icon: 5});
            }
        }

    });
}
$(document).on('click','.select_plot li',function () {
    var communityName =$(this).attr('data-communityName');
    var sid =$(this).attr('data-sId');
    $(".community-head").attr('data-communityName',communityName);
    $(".community-head").attr('data-sId',sid);

});


























